package com.hyk.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.SysPrivilege;
import com.hyk.dto.R;
import com.hyk.service.SysPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@RequestMapping("/privileges")
@Api(tags = "权限配置控制类")
public class SysPrivilegeController {

    @Resource
    private SysPrivilegeService sysPrivilegeService;

    //权限数据分页查询
    @ApiOperation("分页查询权限数据")
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示大小")
    })
    @PreAuthorize("hasAuthority('sys_privilege_query')")
    public R<Page<SysPrivilege>> findByPage(@ApiIgnore Page<SysPrivilege> page){
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysPrivilege> sysPrivilegePage = sysPrivilegeService.page(page);
        return R.ok(sysPrivilegePage);
    }

    //添加一个新的权限
    @ApiOperation("新增权限")
    @PostMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "description",value = "名称"),
            @ApiImplicitParam(name = "menuId",value = "描述"),
            @ApiImplicitParam(name = "name",value = "菜单id")
    })
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    public R add(@RequestBody SysPrivilege sysPrivilege){

        //使用自动填充
//        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
//        sysPrivilege.setCreateBy(Long.valueOf(userId));
//        sysPrivilege.setModifyBy(Long.valueOf(userId));
//        sysPrivilege.setCreated(new Date());
//        sysPrivilege.setLastUpdateTime(new Date());

        boolean save = sysPrivilegeService.save(sysPrivilege);
        if(save){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    //修改一个权限
    //添加一个新的权限
    @ApiOperation("修改权限")
    @PatchMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId",value = "描述"),
            @ApiImplicitParam(name = "name",value = "菜单id")
    })
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    public R update(@RequestBody SysPrivilege sysPrivilege){

//        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
//        sysPrivilege.setModifyBy(Long.valueOf(userId));
//        sysPrivilege.setLastUpdateTime(new Date());

        boolean save = sysPrivilegeService.updateById(sysPrivilege);
        if(save){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    //删除一个权限

}
